<?php
/**
 * @author zhaofei
 * @package 报单审核控制器 
 * @createtime 2014-11-25
 * @updatetime 2014-12-05
 * @ps:ranks 2
 */
namespace System\Controller;
use Think\Controller;
class ExamineController extends Controller {
    private $is_notify_wirter;
    private $is_notify_manager;
	function __construct(){
		parent::__construct();
		$ranks = session('ranks');
		if($ranks!=2){
			$this->error('对不起你没有权限',U('Login/index'));
		}
	}
    
    public function isSend(){
        $rs = D('Config')->find(1);
        $this->is_notify_wirter = $rs['is_notify_wirter'];
        $this->is_notify_manager = $rs['is_notify_manager'];
    }

    public function index(){
    	$conditions = array('id'=>session('id'));
    	$data = D('Employee')->where($conditions)->find();
    	$this->assign('data',$data);
    	$this->display();
    }

    public function order(){
        $cons = array();
        if(isset($_GET['st'])){
            $cons['examize_status'] = $_GET['st'] ; 
        }
        if(isset($_GET['month'])){
            $month_s = date('Y-m');
            $cons['write_time'] = array('between',array(strtotime($month_s),time()));
        }
        $model = M('Order'); // 实例化
        $count = $model->where($cons)->count();// 查询满足要求的总记录数
 
        $Page = new \Think\Page($count,5);// 实例化分页类 传入总记录数和每页显示的记录数(25)
        $show = $Page->show();// 分页显示输出
        // 进行分页数据查询 注意limit方法的参数要使用Page类的属性
        $list = $model->where($cons)->order('write_time desc')->limit($Page->firstRow.','.$Page->listRows)->select();
        $this->assign('list',$list);// 赋值数据集
    
        $this->assign('page',$show);// 赋值分页输出
        $this->display(); // 输出模板
    }


    public function view($id){
        $p = isset($_GET['p'])?$_GET['p']:1;
        $this->assign('p',$p);
        $data = D('Order')->where(array('id'=>$id))->find();
        $this->assign('data',$data);
        $this->display();  
    }

    public function change_status(){
        $p = isset($_GET['p'])?$_GET['p']:1;
        $data = array( 
            'examize_id'=>session('id'),
            'examize_time'=>time()
        );
        if(I('post.reson')){
            $id = I('post.id');
            $data['examize_status']=I('post.status');
            $data['examize_info']=I('post.reson');
        }elseif(I('get.id')){
            $id = I('get.id');
            $data['examize_status']=1;
        }else{
            $this->error('参数错误，审核失败');
            exit();
        }
       $rs = D('Order')->where(array('id'=>$id))->save($data);     
        if($rs){
        $this->isSend();
        if($this->is_notify_wirter){
            $this->notifyWriter($data,$id);
        }
        $this->success('审核成功',U('order',array('p'=>$p)));
       }else{
           $this->error('审核失败');
       }
    }

    public function notifyWriter($data,$id){
        $order = M('Order')->field('e_id,title')
            ->where(array('id'=>$id))->find();
        $writer = D('Employee')->findByAttr(array('id'=>$order['e_id']));
        $href = 'http://'.$_SERVER['HTTP_HOST'];
        $body = "<a href=$href>详情请登陆OA</a>";
        if($data['examize_status']==1){
            $title = $order['title'].'审核通过';
            $body = $title.$body;
        }
        if($data['examize_status'] == 2){ 
            $title = $order['title'].'审核失败';
            $body = $title.$body.$data['examize_info'];
        }
        $w_msg = send_email($writer['email'],$title,$body,true);
        if($w_msg){
            $this->emailLog($writer['email'],$title,$body);
        }
        if($this->is_notify_manager){
            $super = D('Employee')->where(array('ranks'=>1))->find();
            $msg = send_email($super['email'],$order['title'],$body,true);
            if($msg){ 
                $this->emailLog($writer['email'],$title,$body);
            }
        } 
    }

    public function emailLog($to,$title,$body){
        $mail_records = array(
            'from'=>C('Mail_From'),
            'to'=>$to,
            'title'=>$title,
            'body'=>$body,
            'time'=>time(),
        );
        D('Email')->add($mail_records);
    }


    
    //修改密码
    public function password(){
        if(I('post.password')){
            $pass = md5(md5(I('post.password'),true));
            $result = D('Employee')->where(array('id'=>session('id')))->save(array('password'=>$pass));
            if($result){
                $this->success('修改成功，当前用户自动注销',U('Login/logout'));
            }else{
                $this->error('修改失败');
            }
        }else{
            $this->error('密码不能为空');
        }
    }
    



}
